package ldh.im.fxbase.dao;

import ldh.im.fxbase.pojo.ImGroup;
import ldh.im.fxbase.util.DateUtil;
import ldh.im.fxbase.util.DbUtil;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class GroupDao extends BaseDao {

    private static final String TABLE_NAME = "im_group";

    public List<ImGroup> getNewGroups(int size) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select * from im_group order by updateTime desc limit 0, %s", size);
            return queryRunner.query(connection, sql, new BeanListHandler<ImGroup>(ImGroup.class));
        });
    }

    public void saveImGroup(ImGroup imGroup) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            ResultSetHandler<Long> h = new ScalarHandler();
            String sql = String.format("insert into im_group(name, userId, hotMessageTotal, createTime, updateTime) values(?, ?, ?, ?, ?)");
            long n = queryRunner.insert(connection, sql, h, imGroup.getName(), imGroup.getUserId(), imGroup.getHotMessageTotal()
                    , imGroup.getCreateTime(), imGroup.getUpdateTime());
            imGroup.setId(n);
            return n;
        });
    }

    public void increaseNewMessageNum(int groupId, int increaseNum) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            ImGroup imGroup = getBeanById(queryRunner, connection, ImGroup.class, TABLE_NAME, groupId);

            String sql = String.format("update im_group set hotMessageTotal = ?, updateTime=?, version=？ where id = ? and version=？");
            updateBeanById(queryRunner, connection, sql, imGroup.getHotMessageTotal()+increaseNum, new Date(),
                    imGroup.getVersion()+1, groupId, imGroup.getVersion());
            return null;
        });
    }

    public void changeNewMessageNum(int groupId, int hotMessageTotal) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            ImGroup imGroup = getBeanById(queryRunner, connection, ImGroup.class, TABLE_NAME, groupId);
            imGroup.setVersion(imGroup.getVersion() + 1);
            imGroup.setUpdateTime(new Date());

            String sql = String.format("update im_group set hotMessageTotal = ?, updateTime= ?, version= ? where id = ? and version = ?");
            updateBeanById(queryRunner, connection, sql, hotMessageTotal, new Date(),
                    imGroup.getVersion()+1, groupId, imGroup.getVersion());
            return null;
        });
    }

    public void changeNewMessageUpdateTime(Long groupId) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            ImGroup imGroup = getBeanById(queryRunner, connection, ImGroup.class, TABLE_NAME, groupId);
            imGroup.setVersion(imGroup.getVersion() + 1);
            imGroup.setUpdateTime(new Date());

            String sql = String.format("update im_group set updateTime=?, version= ? where id = ? and version = ?");
            updateBeanById(queryRunner, connection, sql, DateUtil.formatNow(),
                    imGroup.getVersion(), groupId, imGroup.getVersion()-1);
            return null;
        });
    }

    public ImGroup getImGroupByUserId(Long id) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select * from im_group where userId = ?", id);
            ImGroup t = queryRunner.query(connection, sql, new BeanHandler<>(ImGroup.class), id);
            return t;
        });
    }

    public ImGroup getImGroupById(Long id) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select * from im_group where id = ?", id);
            ImGroup t = queryRunner.query(connection, sql, new BeanHandler<>(ImGroup.class), id);
            return t;
        });
    }

    public ImGroup getImGroupByName(String name) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select * from im_group where name = ?", name);
            ImGroup t = queryRunner.query(connection, sql, new BeanHandler<>(ImGroup.class), name);
            return t;
        });
    }

    public List<ImGroup> getDefaultGroups() throws SQLException {
        return getNewGroups(100);
    }
}
